<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card>
        <page-tools>
          <template #left>
            <span>共 19 条记录</span>
          </template>
          <template #right>
            <span>
              <el-button v-power="'import'" type="danger" size="small" @click="$router.push('/excel')">导入excel</el-button>
              <el-button v-power="'export'" type="success" size="small" @click="exportExcel">导出excel</el-button>
              <el-button type="primary" size="small" @click="showDialog = true">新增员工</el-button>
            </span>
          </template>
        </page-tools>
      </el-card>
      <!-- 表格主体 -->
      <el-card style="margin-top: 10px;">
        <el-table border :data="employeeList">
          <el-table-column type="index" label="序号" />
          <el-table-column prop="username" label="姓名" />
          <el-table-column label="头像" width="125px">
            <template slot-scope="scope">
              <!-- <img :src="scope.row.staffPhoto" alt="" class="staffPhoto"> -->
              <image-holder :src="scope.row.staffPhoto" class="staffPhoto" />
            </template>
          </el-table-column>
          <el-table-column prop="mobile" label="手机号" width="120px" />
          <el-table-column prop="workNumber" sortable label="工号" />
          <el-table-column prop="formOfEmployment" label="聘用形式" :formatter="formatter" />
          <el-table-column prop="departmentName" label="部门" />
          <el-table-column sortable label="入职时间" width="110">
            <template slot-scope="scope">
              <span>{{ scope.row.timeOfEntry | formatTime }}</span>
            </template>
          </el-table-column>
          <el-table-column label="转正时间" width="100">
            <template slot-scope="scope">
              <span>{{ scope.row.correctionTime | formatTime }}</span>
            </template>
          </el-table-column>
          <el-table-column label="操作" width="280">
            <template slot-scope="scope">
              <el-button type="text" size="small" @click="$router.push(`/employees/detail?id=${scope.row.id}`)">查看</el-button>
              <el-button type="text" size="small" @click="setRoles(scope.row.id)">分配角色</el-button>
              <el-button type="text" size="small" @click="delEmp(scope.row.id)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>

        <!-- 分页组件 -->
        <el-row type="flex" justify="center" align="middle" style="height: 60px">
          <!-- 分页区域 -->
          <el-pagination
            :current-page="query.page"
            :page-sizes="[10, 15, 20, 25]"
            :page-size="query.size"
            layout="total, sizes, prev, pager, next, jumper"
            :total="total"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
          />
        </el-row>
        <!-- 员工新增弹框组件 -->
        <el-dialog
          :close-on-click-modal="false"
          :close-on-press-escape="false"
          title="新增员工"
          :visible.sync="showDialog"
          width="50%"
          @close="DialogClose"
        >
          <emp-dialog ref="empDialog" @close="showDialog = false" @update-employee="updateEmployee" />
        </el-dialog>

        <!-- 分配角色的弹层 -->
        <el-dialog
          title="分配角色"
          :visible.sync="showAssignRoles"
          width="50%"
        >
          <assign-role :user-id="userId" @close="cancelClose" />
        </el-dialog>
      </el-card>
    </div>
  </div>
</template>

<script>
import { getEmployeeList, delEmployee } from '@/api/employee'
import EmpDialog from './EmployeeDialog'
import EmployeeNum from '@/constant/employees'
import dayjs from 'dayjs'
import AssignRole from './assignRoles'
export default {
  name: 'Employees',
  components: {
    EmpDialog,
    AssignRole
  },
  filters: {
  // 格式化时间
    formatTime(time) {
      return dayjs(time).format('YYYY-MM-DD')
    }
  },
  data() {
    return {
      query: {
        page: 1,
        size: 10
      },
      employeeList: [],
      total: 0,
      showDialog: false,
      showAssignRoles: false,
      userId: ''
    }
  },
  created() {
    this.getEmployeeList()
  },
  methods: {
    // 分配角色
    setRoles(id) {
      this.showAssignRoles = true
      this.userId = id
    },
    cancelClose() {
      this.showAssignRoles = false
    },
    // 格式化数据
    transData(rows) {
      const map = {
        'id': '编号',
        'password': '密码',
        'mobile': '手机号',
        'username': '姓名',
        'timeOfEntry': '入职日期',
        'formOfEmployment': '聘用形式',
        'correctionTime': '转正日期',
        'workNumber': '工号',
        'departmentName': '部门',
        'staffPhoto': '头像地址'
      }
      const header = Object.values(map)
      const data = rows.map(item => {
        return Object.values(item)
      })
      return { header, data }
    },
    // 导出excel
    async exportExcel() {
      const res = await getEmployeeList(this.query)
      // console.log(res)
      const { header, data } = this.transData(res.data.rows)
      import('@/vendor/Export2Excel').then(excel => {
        excel.export_json_to_excel({
          header,
          data,
          filename: '员工列表',
          autoWidth: true,
          bookType: 'xlsx'
        })
      })
    },
    DialogClose() {
      this.$refs.empDialog.resetForm()
    },
    // 更新数据
    updateEmployee() {
      this.showDialog = false
      this.getEmployeeList()
    },
    // 格式化聘用形式数据
    formatter(row, column, cellValue, index) {
      // console.log(cellValue)
      const obj = EmployeeNum.hireType.find(item => item.id === cellValue)
      return obj ? obj.value : '未知'
    },
    // 删除员工
    async delEmp(id) {
      const confirmRes = await this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).catch(err => err)
      if (confirmRes === 'cancel') return this.$message('已取消删除')
      const res = await delEmployee(id)
      if (this.employeeList.length === 1) this.query.page--
      if (this.query.page < 1) this.query.page = 1
      this.$message.success(res.message)
      this.getEmployeeList()
    },
    // 获取员工列表
    async getEmployeeList() {
      const res = await getEmployeeList(this.query)
      this.employeeList = res.data.rows
      this.total = res.data.total
    },
    // 分页条数改变
    handleSizeChange(newSize) {
      this.query.size = newSize
      this.getEmployeeList()
    },
    // 分页页码改变
    handleCurrentChange(newPage) {
      this.query.page = newPage
      this.getEmployeeList()
    }
  }
}
</script>

<style lang="scss" scoped>
.staffPhoto {
  width: 100px;
  height: 100px;
  border-radius: 50%;
}
</style>
